package com.pacific.sign.biz.user.service;

import com.pacific.sign.dal.dao.oaSchema.custom.InterfaceOdepartmentCustom;
import com.pacific.sign.dal.dao.oaSchema.custom.InterfaceOemployeeCustom;
import com.pacific.sign.model.po.oaSchema.view.OdePartmentVo;
import com.pacific.sign.model.po.oaSchema.view.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 账户
 */
@Service
public class UserService {

    @Autowired
    private InterfaceOemployeeCustom interfaceOemployeeCustom;
    @Autowired
    private InterfaceOdepartmentCustom interfaceOdepartmentCustom;

    /**
     * 通过工号查询账户
     */
    public UserInfo queryUserByOaCode(String oaCode) {
        //admin系统管理员
        if("1".equals(oaCode)){
            UserInfo admin = new UserInfo();
            admin.setOaCode(1);
            admin.setName("超级管理员");
            admin.setJobcode("0000");
            return admin;
        }
        UserInfo userInfo = interfaceOemployeeCustom.queryUserByOaCode(oaCode);
        if(userInfo != null){
            userInfo.setJobcode(userInfo.getJobcode().substring(userInfo.getJobcode().length()-4,userInfo.getJobcode().length()));
        }
        return userInfo;
    }

    /**
     * 查询下级
     */
    public List<UserInfo> queryLowerLevel(UserInfo req){
        List<UserInfo> userInfos = new ArrayList<UserInfo>();
        UserInfo userInfo = interfaceOemployeeCustom.checkSecondHouse(String.valueOf(req.getOaCode()));
        //为二手房部门
        if(userInfo != null && null != req.getOaCode()){
            //总监查店长（包含代理店长）
            if("5910".equals(req.getJobcode())){
                List<OdePartmentVo> list = interfaceOdepartmentCustom.queryNextDepartment(String.valueOf(111000061));
                if(list != null && list.size() > 0){
                    for(OdePartmentVo vo : list){
                        List<UserInfo> users = interfaceOemployeeCustom.getUsers(vo.getDepcode());
                        if(users != null && users.size() > 0){
                            boolean flag = true;
                            for(UserInfo info : users){
                                String jobCode = info.getJobcode().substring(info.getJobcode().length() - 4, info.getJobcode().length());
                                if("4910".equals(jobCode)){
                                    info.setJobcode(jobCode);
                                    userInfos.add(info);
                                    flag = false;
                                }
                            }
                            if(flag){
                                for(UserInfo info : users){
                                    String jobCode = info.getJobcode().substring(info.getJobcode().length() - 4, info.getJobcode().length());
                                    if("4900".equals(jobCode)){
                                        info.setJobcode(jobCode);
                                        userInfos.add(info);
                                    }
                                }
                            }
                        }
                    }
                }
            }else{
                userInfos = interfaceOemployeeCustom.queryLowerLevel(String.valueOf(req.getOaCode()));
            }
        }
        return userInfos;
    }

    /**
     * 登录
     */
    public UserInfo login(String oaCode) {
        UserInfo userInfo = new UserInfo();
        if(!StringUtils.isBlank(oaCode)){
            oaCode = "11" + oaCode;//查询工号加系统号 11
        }
        userInfo = interfaceOemployeeCustom.queryUserByOaCode(oaCode);
        return userInfo;
    }

    /**
     * 查询店长列表
     */
    public List<UserInfo> listStoreManager(){
        return interfaceOemployeeCustom.listStoreManager();
    }


    /**
     * 查询部门
     */
    public OdePartmentVo queryDepartment(String depCode){
        return interfaceOdepartmentCustom.queryDepartment(depCode);
    }
}
